package org.myframework.rest;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.myframework.jdbc.NativeQuery;
import org.myframework.model.ServiceResult;
import org.myframework.util.MapUtils;
import org.myframework.util.ReloadableProperties;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * INativeQuery 查询的rest服务封装
 * <ol>
 * <li>{@link INativeQuery }</li>
 *
 * </ol>
 *
 * @see
 * @author wanghui
 * @since 1.0
 * @2015年1月27日
 *
 */
@RestController
@RequestMapping("/sqlservice")
public class SqlQueryService {
	private static Log log = LogFactory.getLog(SqlQueryService.class);

	@Resource(name = "nativeQuery")
	private NativeQuery nativeQuery;

	private static String TOTAL = "total";

	private static String ROWS = "rows";

	private static String PAGE = "page";

	private static String ROW_COUNT = "rowCount";

	private static String DATE_TIME = "yyyy-MM-dd hh24:mi:ss";

	private static String DATE_TIME_KEY = "dtFormat";

	private ReloadableProperties propertySource;

	public SqlQueryService() {
		super();
		propertySource = new ReloadableProperties();
		propertySource.setCacheSeconds(15);
		propertySource.setFilename("classpath:application.properties");
	}

	@RequestMapping(value = "/query")
	public Map<String, Object> pageQuery(
			@RequestParam MultiValueMap<String, Object> values,
			@RequestParam String sqlKey) {
		Map<String, Object> requestMap = values.toSingleValueMap();
		requestMap.put(DATE_TIME_KEY, DATE_TIME);

		Map<String, Object> resultMap = values.toSingleValueMap();
		MapUtils<String, Object> rm = new MapUtils<String, Object>(requestMap);
		log.debug("  request  Map :" + requestMap);

		//获取当前页数参数名，获取每页记录数参数名
		String pageKey = getPageKey();
		String pageCountKey = getPageCountKey();
		//返回时的总数参数名,记录集参数名
		String totalKey = getTotalKey();
		String contentKey = getContentKey();
		// 取当前页面和每页记录数
		int currentPage = rm.getInt(pageKey,1);//默认第一页
		int rowCount = rm.getInt(pageCountKey,getDefRowCntPerPage());//默认每页10条

		//执行JDBC查询
		List list = nativeQuery.selectPageList(sqlKey, requestMap,
				(currentPage - 1) * rowCount, rowCount);

		// 返回结果给前端UI
		resultMap.put("success", true);// 当前页
		resultMap.put(pageKey, currentPage);// 当前页
		resultMap.put(pageCountKey, rowCount);// 页记录数
		resultMap.put(contentKey, list); // 当前页数据集
		resultMap.put(totalKey, nativeQuery.selectCount(sqlKey, requestMap));// 总数
		return resultMap;
	}

	@RequestMapping(value = "/pagequery")
	public ServiceResult pageQuery(
			@RequestParam MultiValueMap<String, Object> values,
			@RequestParam String sqlKey,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rowCount) {

		Map<String, Object> requestMap = values.toSingleValueMap();
		requestMap.put(DATE_TIME_KEY, DATE_TIME);
		log.debug("  request  Map :" + requestMap);
		List list = nativeQuery.selectPageList(sqlKey, requestMap, (page - 1)
				* rowCount, rowCount);
		long total = nativeQuery.selectCount(sqlKey, requestMap);
		ServiceResult serviceResult = new ServiceResult();
		serviceResult.setSuccess(true);
		serviceResult.setPageIndex(page);
		serviceResult.setPageCount(rowCount);
		serviceResult.setResultCount(Integer.valueOf(total + ""));
		serviceResult.setContent(list);
		return serviceResult;
	}

	@RequestMapping(value = "/pageDataQuery")
	public List<Map<String, Object>> pageDataQuery(
			@RequestParam MultiValueMap<String, Object> values,
			@RequestParam String sqlKey,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rows) {

		Map<String, Object> requestMap = values.toSingleValueMap();
		requestMap.put(DATE_TIME_KEY, DATE_TIME);
		log.debug("  request  Map :" + requestMap);
		List<Map<String, Object>> list = nativeQuery.selectPageList(sqlKey,
				requestMap, (page - 1) * rows, rows);
		return list;
	}

	@RequestMapping(value = "/getDataTotal", produces = "text/html;charset=UTF-8")
	public String getTotal(@RequestParam MultiValueMap<String, Object> values,
			@RequestParam String sqlKey) {
		Map<String, Object> requestMap = values.toSingleValueMap();
		requestMap.put(DATE_TIME_KEY, DATE_TIME);
		return nativeQuery.selectCount(sqlKey, requestMap).toString();
	}

	@RequestMapping(value = "/allquery", method = RequestMethod.POST)
	public List<Map<String, Object>> allQuery(
			HttpServletRequest request,
			@RequestParam MultiValueMap<String, Object> values,
			@RequestParam String sqlKey,
			@RequestParam(value = "modelname", defaultValue = "") String model_name) {
		Map<String, Object> requestMap = values.toSingleValueMap();
		requestMap.put(DATE_TIME_KEY, DATE_TIME);
		log.debug("  request  Map :" + requestMap);
		List<Map<String, Object>> list = nativeQuery.selectAllList(sqlKey,
				requestMap);
		return list;
	}

	//当前页参数名，每页记录数参数名，返回时的总数参数名，记录集参数名，默认每页10条
	// page_key=current  当前页码
	// pageCount_key=rowCount 每页记录数
	// content_key=rows      查询结果集
	// total_key=total     查询结果总数
	// defRowCntPerPage=20 每页默认记录数

	//获取当前页数参数名，
	private String getPageKey() {
		return propertySource.getProperty("page_key", PAGE);
	}

	//获取页记录数参数名
	private String getPageCountKey() {
		return propertySource.getProperty("pageCount_key", ROW_COUNT);
	}

	//返回时的总数参数名,
	private String getTotalKey() {
		return propertySource.getProperty("total_key", TOTAL);
	}

	//记录集参数名
	private String getContentKey() {
		return propertySource.getProperty("content_key", ROWS);
	}

	//默认每页10条
	private int getDefRowCntPerPage() {
		return Integer.valueOf(propertySource.getProperty("defRowCntPerPage", "10"));
	}
}